import base64
from datetime import datetime

import pytz
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()


class Record(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    label = db.Column(db.String(255), nullable=True)
    position = db.Column(db.String(255), nullable=True)
    zxd = db.Column(db.String(255), nullable=True)
    user = db.Column(db.String(255), nullable=True)
    curtime = db.Column(db.DateTime, nullable=True, default=lambda: datetime.now(pytz.timezone('Asia/Shanghai')))
    img = db.Column(db.LargeBinary, nullable=True)
    bz = db.Column(db.String(255), nullable=True)

    def to_dict(self):
        print(self.img)

        return {
            'id': self.id,
            'label': self.label,
            'position': self.position,
            'zxd': self.zxd,
            'user': self.user,
            'curtime': self.curtime.strftime('%Y-%m-%d %H:%M:%S') if self.curtime else None,
            'img': None,
            'bz': self.bz
        }

    def __repr__(self):
        return f'<Record {self.id}>'
